package cn.client.ui.components

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material3.Card
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.PopupProperties
import cn.client.R
import cn.client.ui.view.main.user.TabContentInUser

@Preview
@Composable
fun info(

) {
    val selectedTabIndex = mutableStateOf(0)
    /* 基本布局 */
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.White)
    ) {
        /* 顶部导航栏 */
        UserTopBar()
        /* 用户card */
        thisUserCard()
        /* 内容区域 */
        TabContentInUser(selectedTabIndex)

    }

}


@Composable
fun thisUserCard(

) {
    Box(
        modifier = Modifier
            .height(380.dp)
            .fillMaxWidth()
    ) {
        Box(
            modifier = Modifier
                .height(230.dp)
                .fillMaxWidth()
        ) {
            // 背景图
            Image(
                painter = painterResource(R.drawable.fc2_nature_meditations),
                contentDescription = null,
                modifier = Modifier
                    .fillMaxWidth()
                    .wrapContentHeight(),
                contentScale = ContentScale.Crop
            )
        }
        // 基本信息
        Box(
            modifier = Modifier
                .padding(top = 210.dp)
                .height(165.dp)
                .fillMaxWidth()
        ) {
            Card(
                modifier = Modifier
                    .height(160.dp)
                    .fillMaxWidth()
                    .padding(5.dp)
            ) {
                Spacer(modifier = Modifier
                    .fillMaxWidth()
                    .height(15.dp))
                // 用户名
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(5.dp)
                ) {
                    Text(
                        text = "userName",
                        fontSize = 25.sp,
                        fontWeight = FontWeight.Bold,
                        modifier = Modifier.padding(start = 110.dp)
                    )
                }
                Spacer(modifier = Modifier
                    .fillMaxWidth()
                    .height(15.dp))
                // 用户邮箱
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(5.dp)
                ) {
                    Text(
                        text = "email:",
                        fontSize = 12.sp,
                        modifier = Modifier.padding(start = 30.dp)
                    )
                }
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(5.dp)
                ) {
                    Text(
                        text = "生日：",
                        fontSize = 12.sp,
                        modifier = Modifier.padding(start = 30.dp)
                    )

                    Text(
                        text = "地区：",
                        fontSize = 12.sp,
                        modifier = Modifier.padding(start = 130.dp)
                    )
                }
            }
        }
        // 头像
        Box(
            modifier = Modifier
                .padding(top = 200.dp, start = 30.dp)
                .height(180.dp)
                .fillMaxWidth()
        ) {
            Image(
                painter = painterResource(R.drawable.ab1_inversions),
                contentDescription = null,
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .size(80.dp)
                    .clip(CircleShape)
                    .border(2.dp, MaterialTheme.colorScheme.primary, CircleShape)

            )
        }

    }
}


@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun UserTopBar(

) {
    data class DropdownItem(val title: String, val id: Int)

    val dropdownList = listOf(
        DropdownItem("设置", 1),
        DropdownItem("退出登录", 2)
    )
    var expanded by remember { mutableStateOf(false) }

    TopAppBar(
        colors = TopAppBarDefaults.topAppBarColors(
            containerColor = MaterialTheme.colorScheme.primaryContainer,
            titleContentColor = MaterialTheme.colorScheme.primary
        ),
        title = { Text("个人信息") },
        /* 左边按钮 */
        navigationIcon = {
//            Icon( painter = painterResource(R.drawable.ic_share_one), null)
            Spacer(modifier = Modifier.size(24.dp))
        },
        /* 右边按钮（可以是多个） */
        actions = {
            IconButton(onClick = { expanded = !expanded }
            ) {
                Icon(Icons.Filled.MoreVert, null)
                /* 下拉框 */
                DropdownMenu(
                    expanded = expanded,
                    onDismissRequest = { expanded = !expanded }, // 当下拉菜单展开/关闭时更新状态
                    modifier = Modifier.wrapContentSize(),
                    properties = PopupProperties(
                        focusable = true,
                        dismissOnBackPress = false,
                        dismissOnClickOutside = true
                    )
                ) {
                    dropdownList.forEach { dropItem ->
                        DropdownMenuItem(
                            { Text(text = dropItem.title) },
                            onClick = {
                                expanded = false
                                when (dropItem.id) {
                                    1 -> { /* 发送意图 */

                                    }

                                    2 -> {  /* 发送意图 */

                                    }

                                    else -> {
                                        Unit
                                    }
                                }

                            }
                        )
                    }
                }
            }
        }
    )
}